package com.sitanyun.merchant.guide.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;

import com.sitanyun.merchant.guide.R;
import com.sitanyun.merchant.guide.base.BaseActivity;
import com.sitanyun.merchant.guide.bean.SwiShopsBean;
import com.sitanyun.merchant.guide.frament.presenter.impl.SwitchstoresAPresenterImpl;
import com.sitanyun.merchant.guide.frament.presenter.inter.ISwitchstoresAPresenter;
import com.sitanyun.merchant.guide.frament.view.inter.ISwitchstoresAView;
import com.sitanyun.merchant.guide.weiht.Aes;
import com.sitanyun.merchant.guide.weiht.ShareLogin;
import com.sitanyun.merchant.guide.weiht.SharedPreferenceUtil;

public class SplashActivity extends BaseActivity implements ISwitchstoresAView {

    private Handler handler;
    private ISwitchstoresAPresenter mISwitchstoresAPresenter;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mISwitchstoresAPresenter = new SwitchstoresAPresenterImpl(this);
        handler = new Handler();

        //延迟 SPLASH_DISPLAY_LENGHT时间然后跳转到MainActivity
        handler.postDelayed(new Runnable() {

            @Override
            public void run() {

                if (SharedPreferenceUtil.getStringData("token").isEmpty()){
                startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                     //未登陆
                    ShareLogin.SaveData("logins","1");
               finish();
                }else {
                    //已经登陆
                    ShareLogin.SaveData("logins","2");
                    Aes mAes = new Aes();
                    try {
                        String secret=  SharedPreferenceUtil.getStringData("cid")
                                +","+SharedPreferenceUtil.getStringData("nodeid")+","
                                +SharedPreferenceUtil.getStringData("staffid")+","
                                +SharedPreferenceUtil.getStringData("userid");
                        byte[]  secrets = secret.getBytes("UTF8");
                        String   secretaec = mAes.encrypt(secrets);//加密
                        mISwitchstoresAPresenter.getswichsave(SharedPreferenceUtil.getStringData("staffid")
                                ,SharedPreferenceUtil.getStringData("userid"),
                                SharedPreferenceUtil.getStringData("cid"),
                                SharedPreferenceUtil.getStringData("nodeid"),secretaec);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }

            }
        }, 3000);
    }

    @Override
    public void initView() {

    }

    @Override
    public <T> T request(int requestFlag) {
        startActivity(new Intent(SplashActivity.this,LoginActivity.class));
        finish();
        return null;
    }

    @Override
    public <T> void response(T response, int responseFlag) {

    }

    @Override
    public <T> void responseshop(T response, int responseFlag) {
        if (responseFlag == ISwitchstoresAView.RESPONSE_ONE) {
            SwiShopsBean loginBean = (SwiShopsBean) response;
            if (loginBean.getCode() == 0) {
                SharedPreferenceUtil.SaveData("userid", loginBean.getUser_id());
                SharedPreferenceUtil.SaveData("nodename", loginBean.getNode_name());
                SharedPreferenceUtil.SaveData("token", loginBean.getAccess_token());
                SharedPreferenceUtil.SaveData("staffid", loginBean.getStaff_id());
                SharedPreferenceUtil.SaveData("clientid", loginBean.getS_client_id());
                SharedPreferenceUtil.SaveData("nodeid",loginBean.getNode_id());
                SharedPreferenceUtil.SaveData("tonodeid", loginBean.getTop_node_id());
                SharedPreferenceUtil.SaveData("sxtoken", loginBean.getRefresh_token());
                SharedPreferenceUtil.SaveData("staffid", loginBean.getStaff_id());
                SharedPreferenceUtil.SaveData("cid", loginBean.getS_client_id());

                startActivity(new Intent(SplashActivity.this,MainActivity.class));
                finish();

            } else {
              startActivity(new Intent(SplashActivity.this,LoginActivity.class));
              finish();
            }

        }else {
            startActivity(new Intent(SplashActivity.this,LoginActivity.class));
            finish();
        }
    }
}